<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Link extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();

		$this->load->model('link_model');
	}

	/**
	 * 列表页面（已审核）
	 */
	public function index()
	{
		$data['links'] = $this->link_model->get_many_by('ifcheck', 1);
		$this->load->view('admin/link_index', $data);
	}

	/**
	 * 排序操作
	 */
	public function do_vieworder()
	{
		$vieworders = $this->input->post('vieworders', TRUE);
		$vieworders === FALSE && show_admin_msg('没有内容要排序');
		//$linkids = $this->input->post('linkids', TRUE);
		//vat_dump($linkids);exit;

		$update = array();
		foreach ($vieworders as $k => $v)
		{
			//$vieworders[$k] = (int)$v;
			$update[] = array('linkid' => (int)$k, 'vieworder' => (int)$v);
		}

		$this->link_model->update_batch($update);

		show_admin_msg('操作成功', 'success');
	}

	/**
	 * 列表页面（未审核）
	 */
	public function check()
	{
		$data['links'] = $this->link_model->get_many_by('ifcheck', 0);
		$this->load->view('admin/link_index', $data);
	}

	/**
	 * 审核操作
	 */
	public function do_check()
	{
		$ifcheck = intval($this->input->get('ifcheck', TRUE));
		$linkids = $this->input->post('linkids', TRUE);
		$linkids === FALSE && show_admin_msg('选择后才允许操作');
		$ifcheck === FALSE && show_admin_msg('非法操作');

		$update = array();
		foreach ($linkids as $linkid)
		{
			//$linkids[$k] = (int)$v;
			$update[] = array('linkid' => (int)$linkid, 'ifcheck' => $ifcheck);
		}

		$this->link_model->update_batch($update);

		show_admin_msg('操作成功', 'success');
	}

	/**
	 * 添加页面
	 */
	public function add()
	{
		$this->load->view('admin/link_add');
	}

	/**
	 * 添加页面(ajax)
	 */
	public function add_ajax()
	{
		$this->output->enable_profiler(FALSE);

		$this->load->view('admin/link_add_ajax');
	}

	/**
	 * 添加操作
	 */
	public function do_add()
	{
		$this->load->library('form_validation');

        //input
		$types = $this->input->post('types', TRUE);
		$name = trim($this->input->post('name', TRUE));
		$url = trim($this->input->post('url', TRUE));
		$logo = trim($this->input->post('logo', TRUE));
		$contact = trim($this->input->post('contact', TRUE));
		$vieworder = (int)$this->input->post('vieworder', TRUE);

        //验证
		$types == FALSE && show_admin_msg('链接类型不能为空');
		empty($name) && show_admin_msg('站点名称不能为空');
		empty($url) && show_admin_msg('链接地址不能为空');

        //数据处理
        $this->load->helper('string');
		$type = count($types) == 2 ? $type = 'all' : $types;
		$name = pw_substr($name, 0, 15);

        //入库数据
		$insert['type'] = $type;
		$insert['name'] = $name;
		$insert['url'] = $url;
		$insert['logo'] = $logo;
		$insert['iflogo'] = $logo ? 1 : 0;
		$insert['contact'] = $contact;
		$insert['vieworder'] = $vieworder;
		$insert['ifcheck'] = 1;
		$insert['created'] = time();
		$insert['updated'] = time();

		$this->link_model->insert($insert);

		//ajax添加时的返回操作
		if ($this->input->is_ajax_request())
		{
			show_admin_msg('操作成功', 'success', site_url('admin/link/index'));
		}

		//非ajax添加时的返回操作
		redirect('admin/link/index');
	}

	/**
	 * 编辑显示页面(ajax)
	 */
	public function edit_ajax()
	{
		$this->output->enable_profiler(FALSE);
		$this->load->helper('form');

		//input
		$linkid = $this->input->get('linkid', TRUE);

		//view
		$data['link'] = $this->link_model->get($linkid);
		$data['linkid'] = $linkid;
		$this->load->view('admin/link_edit_ajax', $data);
	}

	/**
	 * 更新操作(ajax)
	 */
	public function do_edit_ajax()
	{
		//input
		$linkid = intval($this->input->post('linkid', TRUE));
		$types = $this->input->post('types', TRUE);
		$name = trim($this->input->post('name', TRUE));
		$url = trim($this->input->post('url', TRUE));
		$logo = trim($this->input->post('logo', TRUE));
		$contact = trim($this->input->post('contact', TRUE));
		$vieworder = (int)$this->input->post('vieworder', TRUE);
		$created = trim($this->input->post('created', TRUE));
		$ifcheck = intval($this->input->post('ifcheck', TRUE));

		//验证
		$types == FALSE && show_admin_msg('链接类型不能为空');
		empty($name) && show_admin_msg('站点名称不能为空');
		empty($url) && show_admin_msg('链接地址不能为空');

        //数据处理
        $this->load->helper('string');
		$name = pw_substr($name, 0, 15);

		//数据处理
		$type = count($types) == 2 ? $type = 'all' : $types;
		//name 截取15字

		//更新的数据
		$update['linkid'] = $linkid;
		$update['type'] = $type;
		$update['name'] = $name;
		$update['url'] = $url;
		$update['logo'] = $logo;
		$update['iflogo'] = $logo ? 1 : 0;
		$update['contact'] = $contact;
		$update['vieworder'] = $vieworder;
		$update['ifcheck'] = $ifcheck;
		$update['created'] = strtotime($created);
		$update['updated'] = time();

		$this->link_model->update($update);

        if($ifcheck == 1)
        {
            show_admin_msg('操作成功', 'success', site_url('admin/link/index'));
        }
        else
        {
            show_admin_msg('操作成功', 'success', site_url('admin/link/check'));
        }

	}

	/**
	 * 删除操作(单条)
	 */
	public function do_del_ajax()
	{
		$linkid = $this->input->get('linkid', TRUE);
		$linkid == FALSE && show_admin_msg('非法操作');

		$this->link_model->delete($linkid);

		show_admin_msg('操作成功', 'success');
	}

	/**
	 * 删除操作(批量)
	 */
	public function do_dels_ajax()
	{
		$linkids = $this->input->post('linkids', TRUE);
		$linkids == FALSE && show_admin_msg('选择后才允许操作');

		foreach ($linkids as $linkid)
		{
			$this->link_model->delete($linkid);
		}

		show_admin_msg('操作成功', 'success');
	}
}

/* End of file link.php */
/* Location: ./application/controllers/admin/link.php */
